﻿using System;
using UnityEngine;

[RequireComponent(typeof(ParticleSystem)), ExecuteInEditMode]
public class SplineParticlesSystem : MonoBehaviour
{
    private float lastTime;
    public float lerpSpeed = 4f;
    private ParticleSystem m_ps;
    [SerializeField]
    private BezierSpline m_spline;
    private ParticleSystem.Particle[] particles;

    private void LateUpdate()
    {
        if ((this.m_spline != null) && (this.ps != null))
        {
            if ((this.particles == null) || (this.particles.Length < this.m_ps.maxParticles))
            {
                this.particles = new ParticleSystem.Particle[this.m_ps.maxParticles];
            }
            float deltaTime = Time.deltaTime;
            if (!Application.isPlaying)
            {
                deltaTime = Time.realtimeSinceStartup - this.lastTime;
            }
            this.ps.GetParticles(this.particles);
            for (int i = 0; i < this.ps.particleCount; i++)
            {
                ParticleSystem.Particle particle = this.particles[i];
                float num3 = particle.lifetime / particle.startLifetime;
                this.particles[i].position = Vector3.Lerp(particle.position, this.m_spline.Evaluate(1f - num3), deltaTime * this.lerpSpeed);
                this.particles[i].velocity = Vector3.Lerp(particle.position, this.m_spline.EvaluateNormal(1f - num3), deltaTime * this.lerpSpeed);
            }
            this.ps.SetParticles(this.particles, this.ps.particleCount);
            this.lastTime = Time.realtimeSinceStartup;
        }
    }

    public ParticleSystem ps
    {
        get
        {
            return ((this.m_ps != null) ? this.m_ps : (this.m_ps = base.GetComponent<ParticleSystem>()));
        }
    }
}

